﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace AzureDriveUI
{
	public partial class OptionsWindow : Window
	{
		public OptionsWindow()
		{
			InitializeComponent();
		}

		private string _devStorageConnectoinString = "UseDevelopmentStorage=true";

		public ObservableCollection<DriveConfiguration> DriveConfigurations { get; private set; }

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			this.DriveConfigurations = new ObservableCollection<DriveConfiguration>();

			foreach (var item in DriveConfigurationManager.GetConfiguredDrives())
			{
				this.DriveConfigurations.Add(new DriveConfiguration
				{
					ConnectionString = item.ConnectionString,
					DriveLetter = item.DriveLetter,
					VolumeLabel = item.VolumeLabel,
				});
			}

			this.DataContext = this;

			this.Activate();
		}

		private void AddButton_Click(object sender, RoutedEventArgs e)
		{
			var item = new DriveConfiguration();

			foreach (var letter in FileSystemHelper.GetAllAvailableDriveLetters())
			{
				if (this.DriveConfigurations.Any(x => x.DriveLetter == letter) == false)
				{
					item.DriveLetter = letter;

					break;
				}
			}

			if (this.DriveConfigurations.Any(x => x.ConnectionString == _devStorageConnectoinString) == false)
			{
				item.VolumeLabel = "Dev Storage";

				item.ConnectionString = _devStorageConnectoinString;
			}

			this.DriveConfigurations.Add(item);
		}

		private void RemoveButton_Click(object sender, RoutedEventArgs e)
		{
			if (MessageBoxResult.Yes == MessageBox.Show("Are you sure you want to remove this drive configuration?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No))
			{
				var item = ((Button)sender).Tag as DriveConfiguration;

				if (item != null && this.DriveConfigurations.Contains(item))
				{
					this.DriveConfigurations.Remove(item);
				}
			}
		}

		private void SaveButton_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				foreach (var item in this.DriveConfigurations)
				{
					DriveConfigurationManager.SetDriveConfiguration(item.DriveLetter, item.VolumeLabel, item.ConnectionString);
				}

				var configuredDrives = DriveConfigurationManager.GetConfiguredDrives().Select(x => x.DriveLetter).ToList();

				foreach (var driveLetter in configuredDrives.Except(this.DriveConfigurations.Select(x => x.DriveLetter)))
				{
					DriveConfigurationManager.RemoveDriveConfiguration(driveLetter);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Save Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);

				return;
			}

			this.Close();
		}

		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{

		}
	}
}
